home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / prolog1.arc / MATRIX.PRO < prev    next >
Encoding:
Text File  |  1985-07-01  |  1.0 KB  |  45 lines

  1.  
  2.  
  3. con_num_list( [H|T] ) :- rnum( H ), con_num_list( T ).
  4. con_num_list( [] ).
  5.  
  6.  
  7. /* To multiply a row by a column: */
  8.  
  9. mat_mult( [H1|T1], [H2|T2], res ) :-
  10.     mat_mult1( [H1|T1], [H2|T2], 0, res ).
  11.  
  12. mat_mult1( [H1|T1], [H2|T2], sum, res ) :-
  13.     sum is sum + H1 * H2,
  14.     mat_mult1( T1, T2, sum, res ).
  15.  
  16. mat_mult1( [], [], sum, res ) :- res = sum.
  17.  
  18.  
  19. /* To get the nth element of a list: 
  20.    Let us assume a matrix in the form of a list of columns: 
  21.  
  22. [ L1, L2.....]
  23. */
  24.  
  25.  
  26. listel( N, [H|T], X ) :- 
  27.     N > 0, 
  28.     N is N - 1,
  29.     listel( N, T, X ).
  30.  
  31. listel( N, [H|_], X ) :- X = H.
  32.  
  33. /* Below, X, Y are the "coordinates".
  34.    L is the complex list representing the array.
  35.    element is the returned value. */
  36.  
  37. matrix_el( X, Y, L, element ) :-
  38.  
  39. /* First get the row, represented as an element "rowel" in list L */
  40.  
  41.     listel( X, L, rowel ),
  42.  
  43. /* And now the value contained within the row. */
  44.     listel( Y, rowel, element ).
  45.